define(function(require, exports) {
	var c1 = require('c1');

	var c1NewPage = null;

	var c1EditPage = null;

	var c1Page = {};

	c1Page = {
	  init : function() {
		  $(document).on("click", 'form[name="dictionaryForm"] button.c1-query', c1Page.queryClick);

		  c1.form.pagination({
		    paginationSelector : 'div.c1-pagination-load',
		    paginationNoSelector : 'form[name="dictionaryForm"] input[name="paginationNo"]',
		    loadPagination : c1Page.loadPagination
		  });

		  $(document).on("click", '.c1-operation-delete-all', c1Page.deleteAllClick);

		  $(document).on("click", '.c1-operation-import', c1Page.importClick);

		  $(document).on("click", '.c1-operation-new', c1Page.newClick);

		  $(document).on("click", '.c1-operation-edit', c1Page.editClick);

		  $(document).on("click", '.c1-operation-delete-row', c1Page.deleteRowClick);

		  c1.form.joinCheckboxs({
		    mainSelector : 'input[name="checkboxIds"][type="checkbox"]',
		    childSelector : 'input[name="id"][type="checkbox"]'
		  });
	  },
	  importClick : function() {
		  c1.message.progress();

		  var url = cardone.webContextUrl + '/configuration/dictionary/import.html';

		  var $c1Modal = $('.c1-modal');

		  $c1Modal.find('.modal-body').load(url, function() {
			  c1.message.progress('close');

			  require.async([ './import' ], function(page) {
				  if (!c1.importPage) {
					  c1.importPage = page;
				  }

				  c1.importPage.init(c1Page.loadPagination);

				  $c1Modal.find('.modal-title').html('字典导入');

				  $c1Modal.modal('show');
			  });
		  });
	  },
	  queryClick : function() {
		  $('form[name="dictionaryForm"] input[name="paginationNo"]').val(1);

		  c1Page.loadPagination();
	  },
	  loadPagination : function() {
		  var url = cardone.webContextUrl + '/configuration/dictionary/pagination.html';

		  var data = c1.form.serializeJson('form[name="dictionaryForm"]');

		  c1.message.progress();

		  $("div.c1-pagination-load").load(url, data, function() {
			  c1.message.progress('close');

			  $(this).show();

			  var $table = $(this).find('table');

			  if ($table.size() < 1) {
				  $('.c1-operation-btn').hide();

				  return;
			  }

			  $('.c1-operation-btn').show();
		  });
	  },
	  deleteAllClick : function() {
		  var $dictionaryIds = $('input[name="id"]:checked');

		  if ($dictionaryIds.size() < 1) {
			  c1.message.alert({
			    title : '提示消息',
			    message : '请选择需删除的数据！'
			  });

			  return;
		  }

		  var ids = $dictionaryIds.map(function() {
			  return $(this).val();
		  }).get().join(", ");

		  c1Page.deleteRow(ids);
	  },
	  newClick : function() {
		  c1.message.progress();

		  var url = cardone.webContextUrl + '/configuration/dictionary/new.html';

		  var $c1Modal = $('.c1-modal');

		  $c1Modal.find('.modal-body').load(url, function() {
			  c1.message.progress('close');

			  require.async([ './new' ], function(page) {
				  if (c1NewPage == null) {
					  c1NewPage = page;
				  }

				  c1NewPage.init(c1Page.loadPagination);

				  $c1Modal.find('.modal-title').html('字典新增');

				  $c1Modal.modal('show');
			  });
		  });
	  },
	  editClick : function() {
		  c1.message.progress();

		  var url = cardone.webContextUrl + '/configuration/dictionary/edit.html';

		  var $c1Modal = $('.c1-modal');

		  $c1Modal.find('.modal-body').load(url, {
			  'id' : $(this).closest('tr').attr('dictionaryId')
		  }, function() {
			  c1.message.progress('close');

			  require.async([ './edit' ], function(page) {
				  if (c1EditPage == null) {
					  c1EditPage = page;
				  }

				  c1EditPage.init(c1Page.loadPagination);

				  $c1Modal.find('.modal-title').html('字典编辑');

				  $c1Modal.modal('show');
			  });
		  });
	  },
	  deleteRow : function(ids) {
		  c1.message.confirm({
		    title : '确认',
		    message : '将删除选择的数据?',
		    fn : function(r) {
			    if (!r) {
				    return;
			    }

			    var url = cardone.webContextUrl + '/configuration/dictionary/delete.json';

			    c1.message.progress();

			    $.post(url, {
				    'ids' : ids
			    }, function(data) {
				    c1.message.progress('close');

				    if (data.code == '0') {
					    c1.message.alert({
					      title : '提示消息',
					      message : '删除成功',
					      fn : c1Page.loadPagination
					    });

					    return;
				    }

				    c1.message.alert({
				      title : '提示消息',
				      message : '删除失败:' + data.message,
				      fn : c1Page.loadPagination
				    });
			    });
		    }
		  });
	  },
	  deleteRowClick : function() {
		  var dictionaryId = $(this).closest('tr').attr('dictionaryId');

		  c1Page.deleteRow(dictionaryId);
	  }
	};

	require('bootstrap');
	require('bootstrap-typeahead');

	return {
		init : c1Page.init
	};
});